package command

import (
	"desktop-control.gateway.cn/config"
	"desktop-control.gateway.cn/socket"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cobra"
	"log/slog"
	"os"
	"os/signal"
	"sailing.cn/v2/path"
	"syscall"
)

var RootCmd = &cobra.Command{
	Use: path.GetExecPath(),
}

var ServeCmd = &cobra.Command{
	Use:   "serve",
	Short: "启动网关",
	Run:   run,
}

func init() {
	ServeCmd.PersistentFlags().StringVarP(&config.Folder, "dir", "d", "", "配置文件目录")
	RootCmd.AddCommand(ServeCmd)
}

func run(cmd *cobra.Command, args []string) {
	config.Init()
	//client.Init(config.CFG.Services, false)
	//jwt.InitJWKS(config.CFG.Identity.Host)
	//client.Init(config.CFG.Services, true)
	//docs.SwaggerInfo.Title = config.CFG.Webapi.Name
	//docs.SwaggerInfo.Version = config.CFG.Webapi.Version
	//docs.SwaggerInfo.BasePath = config.CFG.Webapi.Prefix
	//docs.SwaggerInfo.Description = "API接口"
	if config.CFG.WebapiConfig.Webapi.Mode != "dev" {
		gin.SetMode(gin.ReleaseMode)
	}
	//var engine = restful.NewGinDefault(
	//	&config.CFG.WebapiConfig,
	//	restful.WithCors(),
	//	//restful.WithSwagger(),
	//	//restful.WithTrace(),
	//)
	s := make(chan os.Signal, 1)
	signal.Notify(s, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGSEGV)
	go socket.Run(fmt.Sprintf(":%d", config.CFG.WebapiConfig.Webapi.Port))
	//go engine.
	//	Router(controller.RegisterRouter).
	//	Router(socket.RegisterWebSocketRouter).
	//	Run()

	<-s
	slog.Info("程序退出")
}
